<?php
class Device 
{
 #constructor
    public function __construct($deviceID, $connection)
    {
        $this->Connection = $connection; 
        if($deviceID!=NULL){
            $this->ID=$deviceID;
            $this->getDefaultInformations();
        }
    return true;
    }
    #end constructor

    #properties
    private $Connection;
    public $DeviceType;     //erledigt
    //Device Informations
    public $ID;             //erledigt
    public $Note;           //erledigt Ausgabe realisieren
    public $Name;           //erledigt
    public $PurchaseDate;   //erledigt Datum umsortieren
    public $Warranty;       //erledigt
    public $Manufacturer;   //erledigt
    public $InventoryNumber;    //erledigt
    public $SerialNumber;   //erledigt
    public $ChangeFlag; //erledigt Ausgabe realisiere selbe wie Note
    
    public $DeviceTypeID;   //erledigt
    public $SupplierID;    //erledigt
    
    //Suppliere Informations
    public $ContactService;     //erledig
    public $ContactSaleOffice;  //erledig
    public $NoteSupplier;   //erledig
    public $NameSupplier;   //erledig
    
    
    //Room and Floor Information
    public $RelocationDate=null; //array //erledig
    public $RoomIs=null;         //array //erledig
    public $RoomIsNumber=null;  //erledig
    public $LevelIs=null;        //array //erledig
    public $LevelShould=null;     //erledig
    public $RoomShould=null; //erledig
    public $PatchPort=null;
    public $Switchport=null;
    public $Mac=null; 
    public $Vlan=null;
    public $VlanName=null;
    public $Switch=null;
    
    private $RelocationID;
    
    //Addition
    public $Additions; //array //erledig

    //Printer Information
    /////
    
public  function getDefaultInformations()
{
    $queries = new mysqlqueries();
    $deviceInformation = $this->Connection->query($queries->GetIndex($queries->Device, $this->ID, null, null));
    if($rowDeviceInformation = mysqli_fetch_array($deviceInformation)) 
    {  
        $this->Name = $rowDeviceInformation['name'];
        $this->PurchaseDate = $rowDeviceInformation['purchaseDate'];
        $this->Warranty = $rowDeviceInformation['warranty'];
        $this->Manufacturer = $rowDeviceInformation['manufacturer'];
        $this->InventoryNumber = $rowDeviceInformation['inventoryNumber'];          
        $this->SerialNumber = $rowDeviceInformation['serialNumber'];
        $this->ChangeFlag = $rowDeviceInformation['changeFlag'];
        $this->DeviceTypeID = $rowDeviceInformation['deviceType_ID'];
        $this->SupplierID = $rowDeviceInformation['supplier_ID'];
    }  
   $deviceTypInformation = $this->Connection->query($queries->GetIndex($queries->Devicetype, $this->DeviceTypeID, null, null));
    if($rowDeviceTypInformation = mysqli_fetch_array($deviceTypInformation)) 
    {
        $this->DeviceType = $rowDeviceTypInformation['description'];
    } 
return true;
}

public function getSupplierInformation()
{
  $queries = new mysqlqueries();
  $supplierInformation = $this->Connection->query($queries->GetIndex($queries->Supplier, $this->SupplierID, null, null));
    if($rowsupplierInformation = mysqli_fetch_array($supplierInformation)) 
    {  
            $this->ContactService = $rowsupplierInformation['contactService'];
            $this->ContactSaleOffice = $rowsupplierInformation['contactSalesOffice'];
            $this->NoteSupplier = $rowsupplierInformation['note'];
            $this->NameSupplier = $rowsupplierInformation['name'];
    }  
return true;
}

public function getInfrastructure()
{        
    $relocationDate = array();
    $roomIs =array();
    $roomIsNumber = array();
    $levelIs= array();
    $levelShould = array();
    $roomShould = array();
    $relocationID = array();

        $queries = new mysqlqueries();
        $relocationInformation = $this->Connection->query($queries->GetIndex($queries->Relocation, $this->ID, "device_ID", "date"));
        while ($rowrelocationInformation = mysqli_fetch_array($relocationInformation)) 
        {
            array_push($relocationDate, $rowrelocationInformation['date']); 
            array_push($relocationID, $rowrelocationInformation['ID']);
            $roomInformation = $this->Connection->query($queries->GetIndex($queries->Room, $rowrelocationInformation['roomIs_ID'], null, null));
            if($rowRoomInformation = mysqli_fetch_array($roomInformation)) 
            { 

                array_push($roomIs, $rowRoomInformation['name']);
                array_push($roomIsNumber, $rowRoomInformation['number']);

                $levelInformation = $this->Connection->query($queries->GetIndex($queries->Level, $rowRoomInformation['level_ID'], null, null));
                if($rowLevelInformation = mysqli_fetch_array($levelInformation)) 
                { 
                    array_push($levelIs, $rowLevelInformation['name']);
                }
            }
            $roomInformation = $this->Connection->query($queries->GetIndex($queries->Room, $rowrelocationInformation['roomShould_ID'], null, null));
            if($rowRoomInformation = mysqli_fetch_array($roomInformation)) 
            { 
                array_push($roomShould, $rowRoomInformation['name']);

                $levelInformation = $this->Connection->query($queries->GetIndex($queries->Level, $rowRoomInformation['level_ID'], null, null));
                if($rowLevelInformation = mysqli_fetch_array($levelInformation)) 
                { 
                    array_push($levelShould, $rowLevelInformation['name']);
                }
            }
        }
        $this->RelocationID = array_reverse($relocationID);
        $this->RelocationDate =  array_reverse($relocationDate);
        $this->RoomIs =  array_reverse($roomIs);
        $this->RoomIsNumber =  array_reverse($roomIsNumber);
        $this->LevelIs =  array_reverse($levelIs);             
        $this->LevelShould = array_reverse($levelShould);
        $this->RoomShould = array_reverse($roomShould);

        $macAddressInformation = $this->Connection->query($queries->GetIndex($queries->Mac, $this->ID, "device_ID", null));
        if($rowMacAddressInformation = mysqli_fetch_array($macAddressInformation))
        {
            $this->Mac = $rowMacAddressInformation['macAddress'];
            $switchPortInformation = $this->Connection->query($queries->GetIndex($queries->Switchport, $rowMacAddressInformation['switchPort_ID'], null, null));
            if($rowSwitchPortInformation = mysqli_fetch_array($switchPortInformation))
            {
                $this->Switchport = $rowSwitchPortInformation['name'];

                $patchPanelInformation = $this->Connection->query($queries->GetIndex($queries->Patchpanel, $rowSwitchPortInformation['ID'], "switchPort_ID", null));
                if($rowPatchPanelInformation= mysqli_fetch_array($patchPanelInformation))
                {
                    $this->PatchPort = $rowPatchPanelInformation['name'];
                }

                $vlanInformation = $this->Connection->query($queries->GetIndex($queries->Vlan, $rowSwitchPortInformation['vlan_ID'], null, null));
                if($rowVlanInformation= mysqli_fetch_array($vlanInformation))
                {
                    $this->Vlan = $rowVlanInformation['vlanNumber'];
                    $this->VlanName = $rowVlanInformation['name'];    
                }

                $switchInformation = $this->Connection->query($queries->GetIndex($queries->Switch, $rowSwitchPortInformation['switch_ID'], null, null));
                if($rowSwitchInformation= mysqli_fetch_array($switchInformation))
                {
                    $this->Switch = $rowSwitchInformation['name'];   
                }
            }
        }
return true;          
}

public function getAdditions()
{
  $additions = array();   
  $queries = new mysqlqueries();
  $additionTypes = $this->Connection->query($queries->GetTable($queries->Additiontype, null)); //get All Additiontypes
    while ($rowadditionTypes = mysqli_fetch_array($additionTypes)) //foreach Additiontype
    {  
        $additionID = array();  
        $additionText = array();
        $additionDate = array();
        $add = false;
        //get all Additions with the device ID and the special AdditionType
        $additionList = $this->Connection->query($queries->GetIndex($queries->Addition, array($rowadditionTypes['ID'], $this->ID), array('additionType_ID', 'device_ID'), 'date'));
        while($rowadditionList = mysqli_fetch_array($additionList)) //Foreach Addition add the information to the arraies
        {
          $add=true;
          array_push($additionDate, $rowadditionList['date']);
          array_push( $additionText, $rowadditionList['text']); 
          array_push($additionID, $rowadditionList['ID']);
        }
        if($add) //If a Addition is found, add this to the additions Array
        {   
        $additionType = $rowadditionTypes['type'];
        $additions[] = array($additionType, array_reverse($additionText), array_reverse($additionDate), array_reverse($additionID));         
        }
    }   
    $this->Additions = $additions;
return true;
}
    
public function UpdateDevice($column,$argument) 
  {
    $queries = new mysqlqueries();
    $this->Connection->query($queries->Update($queries->Device, $argument, $this->ID, $column, null));
    return true;
  }
  
public function UpdateSupplier($column,$argument) 
  {
    $queries = new mysqlqueries();
    $this->Connection->query($queries->Update($queries->Supplier, $argument, $this->SupplierID, $column, null));
    return true;
  }
  
public function UpdateRoom($column, $argument) 
  {
    $queries = new mysqlqueries();
    if(!isset($this->RelocationID[0]))
    {
        $this->Connection->query($queries->Add($queries->Relocation, array(ConvertDate(null, false), $this->ID, null, $argument)));
    }else
    {
        $this->Connection->query($queries->Update($queries->Relocation, $argument, $this->RelocationID[0], $column , null));
    }
    return true;
  }
  
public function UpdateMac($column,$argument) 
{   
    $queries = new mysqlqueries();
    $tempMac = $this->Connection->query($queries->GetIndex($queries->Mac, $this->ID, 'device_ID', null));
    if(mysqli_fetch_array($tempMac))
    {
        $this->Connection->query($queries->Update($queries->Mac, $argument, $this->ID, $column , 'device_ID'));
        $this->Connection->query($queries->Add($queries->Relocation, array(ConvertDate(null, false), $this->ID, null, $argument)));
    }else
    {
        $this->Connection->query($queries->Add($queries->Mac, array($argument, null ,$this->ID)));
    }
  return true;
}
  
public function AddAddition($addition ,$additionDate, $additionType)
{
    $queries = new mysqlqueries();
    $addType= true;
    $additionTypes = $this->Connection->query($queries->GetTable($queries->Additiontype, null));
    while ($rowadditionTypes = mysqli_fetch_array($additionTypes)) 
    {
        if($rowadditionTypes['type'] == $additionType)
        {
            $additionType = $rowadditionTypes['ID'];
            $addType = false;
            break;
        }
    }
    if($addType)
    {
      $this->Connection->query($queries->Add($queries->Additiontype, array($additionType)));
      $additionType = $this->Connection->lastID();
    }
    $this->Connection->query($queries->Add($queries->Addition, array($addition, $additionDate, $this->ID, $additionType)));
    return true; 
}
    
public function DeletAddition($additionID)
{
    $queries = new mysqlqueries();
    $this->Connection->query($queries->Remove($queries->Addition, null, $additionID));
    return true; 
}
  
public function DeletDevice()
{
    $queries = new mysqlqueries();
    $this->Connection->query($queries->Remove($queries->Addition, "device_ID", $this->ID));
    $this->Connection->query($queries->Remove($queries->Relocation, "device_ID", $this->ID));
    $this->Connection->query($queries->Remove($queries->Mac, "device_ID", $this->ID));
    $this->Connection->query($queries->Remove($queries->Device, null, $this->ID));   
    return true; 
    
}

public function UpdateFromCsvMac(){
    $queries = new mysqlqueries();
    $arrgumentsDevice = array($this->Name, $this->InventoryNumber, $this->SerialNumber,  $this->PurchaseDate,  $this->Warranty, $this->Note, $this->Manufacturer,  $this->SupplierID,  $this->DeviceTypeID);
    $columnDevice = array('name','inventoryNumber','serialNumber','purchaseDate','warranty','note','manufacturer','supplier_ID','deviceType_ID');
    
    $this->GetIDByMac();
    $this->Connection->query($queries->Update($queries->Device,$arrgumentsDevice,$this->ID,$columnDevice,NULL));
}//update a device from a csv file refering to mac address

public function UpdateFromCsvName(){
    $queries = new mysqlqueries();
    $arrgumentsDevice = array($this->InventoryNumber, $this->SerialNumber,  $this->PurchaseDate,  $this->Warranty, $this->Note, $this->Manufacturer,  $this->SupplierID,  $this->DeviceTypeID);
    $columnDevice = array('inventoryNumber','serialNumber','purchaseDate','warranty','note','manufacturer','supplier_ID','deviceType_ID');
    
    $this->GetIDByName();
    $this->Connection->query($queries->Update($queries->Device,$arrgumentsDevice,$this->ID,$columnDevice,NULL));
}//update a device from a csv file refering to device name

public function CreateFromCsv(){
    $queries = new mysqlqueries();
    $argument = array(NULL,  $this->Note,$this->Name,  $this->PurchaseDate,  $this->Warranty,$this->Manufacturer, $this->InventoryNumber,  $this->SerialNumber,NULL,  $this->DeviceTypeID,  $this->SupplierID);
    
    $this->Connection->query($queries->Add($queries->Device, $argument));
    $this->ID = $this->Connection->lastID();
}//create a new device from a csv file

public function GetIDByMac(){
    $queries = new mysqlqueries();
    if($this->Mac != ""){
        $rows = $this->Connection->query($queries->GetIndex($queries->Mac, $this->Mac, 'macAddress', null));
        $firstRow = mysqli_fetch_array($rows);
        $this->ID = $firstRow['device_ID'];
    }
}//gets the matching Device ID to an existing MAC address

public function GetIDByName() {
    $queries = new mysqlqueries();
    if($this->Name != ""){
        $rows = $this->Connection->query($queries->GetIndex($queries->Device, $this->Name, 'name', null));
        $firstRow = mysqli_fetch_array($rows);
        $this->ID = $firstRow['ID'];        
    }
}//gets the matching Device ID to an existing Name

public function AddMac(){
    $queries = new mysqlqueries();
    if($this->Mac != ""){
        $argument=array(NULL,  $this->Mac, NULL, $this->ID);
        $this->Connection->query($queries->Add($queries->Mac, $argument));
    }
}//adds a new mac Adress
}